<?php

namespace app\admin\controller;

use app\admin\model\Users;
use think\Controller;
use think\Db;
use think\Request;
use think\Validate;

class User extends Controller
{
    /**
     * 显示资源列表
     *
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $userModel= new Users();
        $userLists =$userModel->select();
//        $this->assign([
//            'user_list'=>$userLists,
//        ]);
//        return $this->fetch();
        return view('user/index',[
            'user_list'=>$userLists

        ]);
    }

    public function userAdd()
    {
        return view('user/add');
    }


    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create(Request $request)
    {
        //
        $param =input('post.');
        $rule = [
            ['username','require|min:2','用户名必须写'],
            ['passwd','require|min:5','密码必须|密码至少5位']
        ];
        $validate =new Validate($rule);
        $result =$validate->check($param);
//        var_dump($result);
        if(!$result){
            return json(['code'=>-1,'data'=>'','msg'=>$validate->getError()]);
        }

        $userModel =new Users();
        $param['passwd']=md5($param['passwd']);
//        $flag = true;
        $flag =$userModel->insert($param);
//        var_dump($flag);
        if(empty($flag)){
            return json(['code'=>-2,'data'=>'','msg'=>'系统错误']);

        }

        return json(['code'=>1,'data'=>'','msg'=>'添加用户成功']);

    }


    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        echo 'save';
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
        if(request()->isPost()){

            $param =input('post.');
            if(empty($param['username']))
            {
                return json(['code'=>-1,'data'=>'','msg'=>'用户名不能为空']);

            }
            $editParam =[
                'username'=>$param['username']
            ];

            $userModel =new Users();
            $has =$userModel->field('id')->where("username='".$param['username']."'and id !=".$param['id'])->find();
            if(!empty($has))
            {
                return json(['code'=>-2,'data'=>'','msg'=>'该用户已经存在']);

            }
            if(!empty($param['old_user_pass']) && empty($param['new_user_pass']))
            {
                return json(['code'=>-3,'data'=>'','msg'=>'修改密码需要输入新密码']);

            }
            if(empty($param['old_user_pass']) && !empty($param['new_user_pass']))
            {
                return json(['code'=>-4,'data'=>'','msg'=>'旧密码不对']);

            }
            if(!empty($param['old_user_pass']) && !empty($param['new_user_pass']))
            {
                if(strlen($param['new_user_pass'])<5)
                {
                    return json(['code'=>-5,'data'=>'','msg'=>'新密码不少于5为']);

                }
                $userInfo =$userModel->where('id',$param['id'])->find();
                if(md5($param['old_user_pass']) != $userInfo->passwd)
                {
                    return json(['code'=>-6,'data'=>'','msg'=>'旧密码不对']);

                }
                $editParam['passwd']=md5($param['new_user_pass']);

                $flag = $userModel->where('id',$param['id'])->update($editParam);
                if(false === $flag)
                {
                    return json(['code'=>-7,'data'=>'','msg'=>'系统错误']);
                }

                return json(['code'=>1,'data'=>'','msg'=>'修改成功']);

            }

        }
        $id =input('param.id');
        $userModel = new Users();
        $userInfo =$userModel->where('id',$id)->find();
        return json(['code'=>1,'data'=>$userInfo,'msg'=>'user information']);

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
        $id =input('param.id');
        $userModel =new Users();
        $flag =$userModel->where('id',$id)->delete();
        if(false === $flag){
            return json(['code'=>-1,'data'=>'','msg'=>'系统错误']);
        }

        return json(['code'=>1,'data'=>'','msg'=>'删除成功']);

    }
}
